<template>

  <el-form :model="realname" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
    <el-card class="box-card">
      <span>基本信息</span>
      <div class="text item">
        <el-form-item label="订单号">
          <el-input v-model="tableData.orderInfo.orderNumber" disabled></el-input>
        </el-form-item>
        <el-form-item label="申请时间">
          <el-input :value="getDate(tableData.orderInfo.addTime)" disabled></el-input>
        </el-form-item>

        <el-form-item label="订单状态">
          <el-input v-model="tableData.orderInfo.orderStatusString" disabled></el-input>
        </el-form-item>
        <el-form-item label="汽车总数量">
          <el-input v-model="tableData.orderInfo.carSumNumber" disabled></el-input>
        </el-form-item>
        <el-form-item label="融资经销商">
          <el-input v-model="tableData.orderInfo.dealerName" disabled></el-input>
        </el-form-item>
        <el-form-item label="融资总金额(元)">
          <el-input :value="tableData.orderInfo.mortgageSumMoney| toFixed2"  disabled></el-input>
           <span class="convertCurrency">{{tableData.orderInfo.mortgageSumMoney|convertCurrency}}</span>
        </el-form-item>

        <el-form-item label="初始融资周期">
          <el-input v-model="tableData.orderInfo.description" disabled></el-input>
        </el-form-item>


        <el-form-item label="初始融资利率">
          <el-input v-model="tableData.orderInfo.mortgageInterestRate" disabled></el-input>
        </el-form-item>

        <el-form-item label="业务员">
          <el-input v-model="tableData.orderInfo.businessSysUserName" disabled></el-input>
        </el-form-item>

        <el-form-item label="收款账户名">
          <el-input v-model="tableData.orderInfo.accountName" disabled></el-input>
        </el-form-item>
        <el-form-item label="收款账号">
          <el-input v-model="tableData.orderInfo.accountNumber" disabled></el-input>
        </el-form-item>
        <el-form-item label="开户行">
          <el-input v-model="tableData.orderInfo.openningBank" disabled></el-input>
        </el-form-item>

        <el-form-item label="资源审核意见">
          <el-input v-model="tableData.orderInfo.resourceAuditOpinion" :maxlength='100' disabled></el-input>
        </el-form-item>


        <el-form-item label="物管验车意见">
          <el-input v-model="tableData.orderInfo.propertyInspectionOpinion" :maxlength='100' disabled></el-input>
        </el-form-item>
        <el-form-item label="风控验车意见">
          <el-input v-model="tableData.orderInfo.riskAuditOpinion" :maxlength='100' disabled></el-input>
        </el-form-item>

        
        <el-form-item label="资金方" prop='funderUser '>
          <el-select v-model="realname.funderUser " placeholder="下拉选择" @change="getSpvUser" :disabled='isCreateContract'>
            <el-option  label="请选择资金方" value="0" ></el-option>
            <el-option v-for="item in realname.realname" :key="item.id" :value='item.id' :label="item.realname" >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="SPV">
          <!-- <el-select v-model="spv" placeholder="下拉选择">
            <el-option   :label="spv"  :value="spv" >
              
            </el-option>
          </el-select> -->
           <el-input v-model="spv" disabled></el-input>
        </el-form-item>
        <el-form-item v-if="true" label="合同信息">
          <el-transfer v-model="contractValue" :data="contractData" :titles="contractTitles" @change="contractChange"></el-transfer>
        </el-form-item>
      </div>
    </el-card>
    <el-form-item class='align'>
      <el-button type="primary"  @click="resetForm('ruleForm',$event)">返回</el-button>
      <el-button 
        type="success"
        @click="submitForm('ruleForm',$event)"
        >提交</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
  import qiniu from '@/views/qiniu/upload';
  import {
    require,
    prop,
    decimalTwo
  } from '@/utils/rules.js' //表单验证规则
  import {
    timestampToTime
  } from '@/utils/index.js';
  import agentHttp from '@/utils/agentHttp.js';
  export default {
    data() {
      return {
        ruleForm: {
          valueMoney: '',
          valueSPV: ''
        },
        rules: {
          valueMoney: [require("资金方")],
          valueSPV: [require("SPV")],
        },
        radio: '1',
        contractData: [], //合同信息
        contractTitles: ['可选合同模板', '已选合同模板'],
        contractValue: [],
        tableData: {},
        orderInfo:{},
        realname:{funderUser:'',realname:[]},  //资金方信息
        spv:'',       //spv信息
        index:null,   //提交合同时，需要对应的索引
        isCreateContract:'',  //是否生成过合同
         packJson2:{
						maxLength:10,   
						accept:"",
						size:4    
					}
      };
    },
    methods: {
      submitForm(formName,e) {
        this.$refs[formName].validate(valid => {
          if (valid) {
                var params = {
                    orderId:this.orderId
                }
                params.contractTemplateList =this.contractValue ;     //传给后台的合同模板id
                // params.contractTemplateList = [] ;     //传给后台的合同模板id
                
                switch(true){
                  case this.index !== null :
                            const funderId =  this.realname.realname[this.index].funderId 
                            const spvId = this.realname.realname[this.index].spvId ;
                            params.funderId =funderId
                            params.spvId =spvId  
                            break ;
                  default :
                            const arr =  this.realname.realname.filter((item,index)=>{
    
                                      return item.funderId ==  this.orderInfo.funderId
                                  })
                            params.funderId =arr[0].funderId
                            params.spvId =arr[0].spvId  
                             break
                  
                }
                // console.dir(this.orderInfo.funderId)
                params.funderId&&params.contractTemplateList.length?"": this.$message.warning(`请至少选择一个合同模板`)

                if(   params.contractTemplateList.length>0&&params.funderId!=null){
                    agentHttp.post(`adminApi/admin/mortgage_contractinfo/add_contract_list`, params).then(res=>{
                        if(res.success){
                                if(res.data.length!=0){
                                      res.data.map(item=>this.$message.warning( item.message))
                                }else{
                                      this.dialogSendData.dialogFormVisible = false;
                                      this.$message.success( "生成合同成功...");
                                      this.$emit('Refresh',null);  
                                    
                                }
                        }
                    })
                }
                
           
          } else {
            console.log("error submit!!");
            return false;
          }
          
             
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
        this.dialogSendData.dialogFormVisible = false;
      },

      initData() {
        agentHttp.get(`adminApi/admin/mortgage_contractinfo/get_order_mortgage_contract_info/${this.orderId}`, {}).then(
          res => {
            if (res.success) {
              this.tableData = res.data;
               res.data.funderUser.map((items,index)=>{                 
                  this.realname.realname.push({        //取出资金方信息
                    id:index,
                    funderId:items.id,
                    spvId:items.spvId,
                    realname: items.realname,
                    spv:items.spv ? items.spv.realname:null
                  })  
               })
              this.orderInfo = res.data.orderInfo ;   //订单信息  
              this.isCreateContract = res.data.isCreateContract ;   //是否生成过合同
              this.getSpvUser()
            }
          })
      },
      getDate(time) {
        return timestampToTime(time)
      },
      getSpvUser(index){ 
         this.contractData = [] ;
          if(index||index==0){
              this.spv = this.realname.realname[index].spv
              this.index = index ;
              const funderId =  this.realname.realname[this.index].funderId ;           
              const id = this.orderInfo.carOrderId ;  
              const arr = [] ;
                agentHttp.get(`adminApi/admin/contractinfo/get_order_contract_info_get_template/${funderId}/${id}`).then((data)=>{  
                        if(data.success){  
                            data.data.map((item,index)=>{       //  筛选出合同
                              arr.push( {
                                  'key':item.id,
                                  'label':item.name
                                })
                                this.contractData = arr;
                            })
                        } 
                  }); 
                
            }else{
              var funderId =  this.orderInfo.funderId 
              var id = this.orderInfo.carOrderId ;
              const arr=[] ;
              switch(true){
                  case  new Boolean(funderId)==true : 
                      //默认显示已有的资金方合同
                        agentHttp.get(`adminApi/admin/contractinfo/get_order_contract_info_get_template/${funderId}/${id}`).then((data)=>{  
                                if(data.success){  
                                    data.data.map((item,index)=>{       //  筛选出合同
                                        arr.push({
                                          'key':item.id,
                                          'label':item.name
                                        })
                                    })
                                    this.contractData = arr; 
                                }
                          }); 
                          //通过funderID匹配出资金方和spv
                            const defaultFunder  =  this.realname.realname.filter((item,index)=>{
                                return item.funderId == funderId
                            })
                            this.realname.funderUser =  defaultFunder[0].realname ;
                            this.spv = defaultFunder[0].spv ;
                                  break ;
                  default :
                                  break ;

              }
              
            }
			},
    },
    props: {
      dialogSendData: {
        type: Boolean
      },
      orderId: {
        type: Number
      }
    },
    mounted() {
      this.initData()
    },
    components: {
      qiniu
    }
  };

</script>

<style lang="scss" scoped>
  .text {
    font-size: 14px;
  }

  .item {
    margin-bottom: 18px;
  }

  .clearfix:before,
  .clearfix:after {
    display: table;
    content: "";
  }

  .clearfix:after {
    clear: both;
  }

  .box-card {
    margin-top: 20px;
  }

  .align {
    margin: 50px auto 0;
    text-align: center;
  }

</style>

